#include <stdio.h>
#include "ParallaxLayer.h"

ParallaxLayer::ParallaxLayer(TextureManager * textureManager, const char * textureFile, Vector2D scrollRatio,
							 Vector2D size, Vector2D position, Vector2D boxSize, Vector2D boxPosition)
{
	this->textureManager = textureManager;
	this->scrollRatio = scrollRatio;
	this->size = size;
	this->position = position;
	this->boxSize = boxSize;
	this->boxPosition = boxPosition;
	scroll = Vector2D(0,0);
	textureIndex = 0;
	if(this->textureManager)
	{
		if(!this->textureManager->LoadTexture(textureFile))
		{
			printf("Failed to load %s\n", textureFile);
		}
		else
		{
			textureIndex = textureManager->TextureIndex(textureFile);
		}
	}
}
ParallaxLayer::~ParallaxLayer()
{
}
void ParallaxLayer::Draw()
{
	if(textureManager)
	{
		textureManager->EnableTexture(textureIndex);
	}

	glAlphaFunc( GL_GREATER, 0.5 );
  	glEnable( GL_ALPHA_TEST );
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_COLOR, GL_DST_COLOR);
	glBegin(GL_QUADS);
	glTexCoord2f(scroll.x + position.x, scroll.y  + position.y + size.y);
	glVertex2f(boxPosition.x, boxPosition.y + boxSize.y);
	
	glTexCoord2f(scroll.x +position.x + size.x, scroll.y  + position.y + size.y);
	glVertex2f(boxPosition.x + boxSize.x, boxPosition.y + boxSize.y);
	
	glTexCoord2f(scroll.x + position.x + size.x, scroll.y + position.y);
	glVertex2f(boxPosition.x + boxSize.x,boxPosition.y);
	
	glTexCoord2f(scroll.x + position.x, scroll.y + position.y);
	glVertex2f(boxPosition.x, boxPosition.y);
	glEnd();
	glDisable(GL_BLEND);
}
void ParallaxLayer::Think(Vector2D cameraTranslation)
{
	scroll.x += cameraTranslation.x * scrollRatio.x;
	scroll.y += cameraTranslation.y * scrollRatio.y;
}	

	